<?php
namespace app\index\controller;
use think\Db;
use think\Controller;
use think\Session;
use think\Request;
use think\Cookie;


class Index extends Error
{   
    private $uid;
    public function _initialize()
    {
        ( empty(cookie::get('loginid')) && empty(session::get('user')) )? $uid = '' : 
            (!empty(session::get('user'))? $uid = session::get('user.id') : $uid = cookie::get('loginid'));
        $this->uid = $uid;
    }

    public function index()
    {   
        if(!empty($this->uid)){
             $res = Db::field('d.name')->table(['s75_data'=>'d','s75_orders'=>'o'])
                ->where('d.id=o.cid')->where('o.status=0')->where('o.uid='.$this->uid)->select();
         cookie::set('count',count($res));
        }
       
        // 热门推荐
        $hot = db('data')->field(['id','name','face','price'])->order('hot desc')->limit(12)->select();
        // 最新上线
        $new = db('data')->field(['id','name','face','price'])->order('ctime')->limit(12)->select();
        // 最受好评
        $score = db('data')->field(['id','name','face','price'])->order('score')->limit(12)->select();
        // 经典推荐
        $search =  db('data')->field(['id','name','face','price'])->order('search')->limit(12)->select();


        //todo:查询登陆的用户是否是作者
        $loginid = Cookie::get('loginid');
        $user = Session::get('user');

        $point = db('author_user')->where('uid',$user['id'])->find();
        $point1 = db('author_user')->where('uid',$loginid)->find();
//        var_dump($point);
//        var_dump($point1);
//        die;
         Session::set('point',$point);
         Cookie::set('point1',$point1);





        //  友情链接  
        $link = $this->bookList();
        $this->assign('hot',$hot);
        $this->assign('new',$new);
        $this->assign('score',$score);
        $this->assign('search',$search);
        return view('index/index',[
            'title'=>'新芽阅读',
            'hot'=>$hot,
            'new'=>$new,
            'score'=>$score,
            'search'=>$search,
            'link' => $link,
        ]);
    }


    //todo:遍历所有的出版社和图书
    public function bookList()
    {
        $bookconcern = db('link')->field(['id', 'name','icon','status'])->where('pid', '=', '0')->where('status','1')->select();
        $bookname = db('link')->field(['id', 'pid', 'name','icon','status'])->where('pid', '>', '0')->where('status','1')->select();

        foreach ($bookconcern as $k => $v) {
            $bookconcern[$k]['list'] = [];
            foreach ($bookname as $ko => $vo) {
                if ($v['id'] == $vo['pid']) {
                    $bookconcern[$k]['list'][] = $vo;
                }
            }
        }
        return $bookconcern;
    }


    // 搜索图书
    public function find(Request $request)
    {   
        $res = $request->post();
        $search = $res['search'];
        // 搜索结果
        $res = db('data')->field(['id','name','face','price','author','tag'])
            ->where('name','like','%'.$search.'%')->whereor('author','like','%'.$search.'%')->select();
        if ($res) {
             foreach ($res as $k => $v) { $ids[] = $v['id']; }
            // 搜索量 +1
            db('data')->where('id','in',$ids)->setInc('search');
        }
        // 热门推荐
        $recom =  db('data')->field(['id','name','face','author','price'])->order('search desc')->limit(4)->select();
        // 其他购买
        $other =  db('data')->field(['id','name','face','price'])->order('sale desc')->limit(6)->select();
        return view('index/search',[
            'title'=>'搜索——xxx',
            'search'=>$search,
            'res'=>$res,
            'recom'=>$recom,
            'other'=>$other
        ]);
    }

    public function api()
    {
        $data = db('data')->field(['id','name','face','author','price'])->limit(6)->select();
        return json($data);
    }

}